package ma.strutsfacile.download;

import org.apache.struts2.dispatcher.StreamResult;

import com.opensymphony.xwork2.ActionInvocation;

/**
 * This class for result-type="myStream"
 * 
 * 
 * <result-types> <result-type name="myStream" default="false"
 * class="downloadexample.DynamicStreamResult"/>
 * 
 * </result-types>
 * 
 * 
 * It extends StreamResult Used to download file as a stream.
 * 
 * @author sheetal
 * 
 */

public class DynamicStreamResult extends StreamResult {
    protected void doExecute(String finalLocation, ActionInvocation invocation)
	    throws Exception {

	// get name of downloaded file
	String downloadedFileName =  ""+ invocation.getStack().findValue( conditionalParse("name", invocation));

	contentDisposition = "filename=\"" + downloadedFileName + "\"";

	// get file size
	contentLength = ""
		+ invocation.getStack().findValue(
			conditionalParse("size", invocation));
	System.out.println("contentLength="+contentLength);
	// get type of file
	contentType = ""
		+ invocation.getStack().findValue(
			conditionalParse("contentType", invocation));
	inputName = "inputStream";
	System.out.println("contentType="+contentType);
	/*
	 * Executes the result given a final location (jsp page, action, etc)
	 * and the action invocation (the state in which the action was
	 * executed).
	 */
	super.doExecute(finalLocation, invocation);
    }
}